/**
 * 界面组件维护 - 编辑页面
 * @author miaoyl
 * @date 2018/01/02 09:35
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            detail: false,
            form: '',
            rules: {
                MODULE_NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                NAME: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                DOM_ID: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                DOM_TYPE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                DOM_PERSSION: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            }
        },
        callObj,
        pageVue
    ;

    var CmpEdit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'cmp_edit'
            }, win);
        },
        editCmp: function () {
            app.ajax({
                xurl: 'sys/cmp/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '界面组件修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        addCmp: function () {
            app.ajax({
                xurl: 'sys/cmp/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '界面组件信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('cmp').reloadTableCurr();
                        layer.close(layer.index);
                        //self._closeTpl();
                    }
                }
            });
        },
        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            vueData.detail = false;
            if (obj.is_add) {
                vueData.isAdd=true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        PAGE_ID: obj.page_id,
                        MODULE_NAME: obj.module_name
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = obj.obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                if(obj.detail){
                    vueData.detail = true;
                }else{
                    vueData.detail = false;
                }
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['sys_cmp']);
            pageVue = new Vue({
                el: '#cmp_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        if(vueData.detail) {
                            layer.close(layer.index);
                        }else {
                            self._closeTpl();
                        }
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addCmp();
                                } else {
                                    self.editCmp();
                                }
                            }
                        });
                    }
                },
                watch: {},
                created: function () {
                    self._setEleIsReady('cmp_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });

    return new CmpEdit();

});